package com.cogent.business.session;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

public class SessionUtil {
	private static String SEESSION_PREFIX = "REQUEST_SESSION_NOTE_";
	public static void setRequestMsg(HttpServletRequest request, String key, Object value) {
		request.getSession().setAttribute(key, value);
	}
	
	public static Object getReuestMsg(HttpServletRequest request, String key) {
		Object obj = request.getSession().getAttribute(key);
		if(obj != null)
			request.getSession().removeAttribute(key);
		return obj;
	}
	
	public static void setNoteMsg(HttpServletRequest request, String formName, String notemsg) {
		String key = SEESSION_PREFIX + formName;
		request.getSession().setAttribute(key, notemsg);
	}
	
	public static String getNoteMsg(HttpServletRequest request, String formName) {
		String ret = "";
		String key = SEESSION_PREFIX + formName;
		Object obj = request.getSession().getAttribute(key);
		if(obj != null)
			ret = obj.toString();
		clearSessionByPrefix(request, SEESSION_PREFIX);
		
		return ret;
	}
	
	private static void clearSessionByPrefix(HttpServletRequest request, String prefix) {
		Enumeration para = request.getSession().getAttributeNames();
		while (para.hasMoreElements()) {
		    String name=(String)para.nextElement();
		    if(name.indexOf(SEESSION_PREFIX) == 0)
		    	request.getSession().removeAttribute(name);
		}
	}
}
